from flask import Flask,jsonify,request,Blueprint,session
from common.res import (getRes,getErrorRes,getSuccessRes,getDataRes)
from common.sqlInjectionSecurity import sqlEscape,sqlEscapeField
from DAL.admDAL import cartDAL
from controller.logAttr import log
from controller.public_param import MODEL_DOWN_DIR,MODEL_UPLOAD_DIR
import os

adm_cart = Blueprint('adm_cart',__name__)


@adm_cart.route("/adm_cart/getlist",methods=['POST'])
def getlist():
    param = {
        'start': request.form.get('start') and sqlEscape(request.form.get('start')) or None,
        'end': request.form.get('end') and sqlEscape(request.form.get('end')) or None,
        'phone_num': request.form.get('phone_num') and sqlEscape(request.form.get('phone_num')) or "",
        'belong_to': request.form.get('belong_to') and sqlEscape(request.form.get('belong_to')) or None,
        'tel_traffic': request.form.get('tel_traffic') and sqlEscape(request.form.get('tel_traffic')) or None,
        'tel_type': request.form.get('tel_type') and sqlEscape(request.form.get('tel_type')) or None,
        'page': request.form.get('page') and sqlEscape(request.form.get('page')) or 1,
        'limit': request.form.get('limit') and sqlEscape(request.form.get('limit')) or 20,
        'filed': request.form.get('filed') and sqlEscape(request.form.get('filed')) or 'id',
        'sort': request.form.get('sort') and sqlEscape(request.form.get('sort')) or 'ascending'
          }
    dal = cartDAL()
    data,count = dal.getList(**param)
    if data:
        res = getSuccessRes(data=data,count=count)
    else:
        res = getSuccessRes(data=[], count=count)
    return jsonify(res)


@adm_cart.route("/adm_cart/getOneByNum",methods=['POST'])
def getOneByNum():
    param = {
        'phone_num': request.form.get('phone_num') and sqlEscape(request.form.get('phone_num')) or ""
          }
    if not param['phone_num']:
        return getSuccessRes(data=None)

    dal = cartDAL()
    data = dal.getOneByNum(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)

@adm_cart.route("/adm_cart/isExist_num",methods=['POST'])
def isExist_num():
    param = {
        'phone_num': request.form.get('phone_num') and sqlEscape(request.form.get('phone_num')) or ""
          }
    dal = cartDAL()
    data = dal.isExist_num(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)

@adm_cart.route("/adm_cart/delete_by_id",methods=['POST'])
@log(api_url="/adm_cart/delete_by_id")
def delete_by_id():
    param = {
        'id': request.form.get('id') or None
    }
    if not param['id']:
        res = getErrorRes("你输入的参数有错！")
        return jsonify(res)
    dal = cartDAL()
    data = dal.deleteById(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)



@adm_cart.route("/adm_cart/notExistAdd",methods=['POST'])
@log(api_url="/adm_cart/notExistAdd")
def add():
    param = {
        'phone_num': request.form.get('phone_num') and sqlEscape(request.form.get('phone_num')) or "",
        'belong_to': request.form.get('belong_to') and sqlEscape(request.form.get('belong_to')) or None,
        'tel_package': request.form.get('tel_package') and sqlEscape(request.form.get('tel_package')) or None,
        'tel_traffic': request.form.get('tel_traffic') and sqlEscape(request.form.get('tel_traffic')) or None,
        'tel_type': request.form.get('tel_type') and sqlEscape(request.form.get('tel_type')) or None,
        'discount_exp_date': request.form.get('discount_exp_date') and sqlEscape(request.form.get('discount_exp_date')) or None,
        'wechat_pwd': request.form.get('wechat_pwd') and sqlEscape(request.form.get('wechat_pwd')) or None,
    }
    if not param['phone_num']:
        res = getErrorRes("手机号码必须填写！")
        return jsonify(res)
    dal = cartDAL()
    data = dal.notExistAdd(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)


@adm_cart.route("/adm_cart/update",methods=['POST'])
@log(api_url="/adm_cart/update")
def update():
    param = {
        'id': request.form.get('id') or None,
        'phone_num': request.form.get('phone_num') and sqlEscape(request.form.get('phone_num')) or "",
        'belong_to': request.form.get('belong_to') and sqlEscape(request.form.get('belong_to')) or None,
        'tel_package': request.form.get('tel_package') and sqlEscape(request.form.get('tel_package')) or None,
        'tel_traffic': request.form.get('tel_traffic') and sqlEscape(request.form.get('tel_traffic')) or None,
        'tel_type': request.form.get('tel_type') and sqlEscape(request.form.get('tel_type')) or None,
        'discount_exp_date': request.form.get('discount_exp_date') and sqlEscape(request.form.get('discount_exp_date')) or None,
        'wechat_pwd': request.form.get('wechat_pwd') and sqlEscape(request.form.get('wechat_pwd')) or None,
    }
    if not param['id']:
        res = getErrorRes("参数id必须填写！")
        return jsonify(res)
    if not param['phone_num']:
        res = getErrorRes("手机号码必须填写！")
        return jsonify(res)
    dal = cartDAL()
    data = dal.update(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)

@adm_cart.route("/adm_cart/batch_processing/<path:filename>",methods=['POST'])
@log(api_url="/adm_cart/batch_processing")
def batch_processing(filename):
    random_integer = request.form.get('random') or None
    if not random_integer:
        res = getErrorRes("操作有错！")
        return jsonify(res)
    url =os.path.join(MODEL_UPLOAD_DIR,random_integer+filename)
    dal = cartDAL()
    data = dal.batch_processing(file_url=url)
    res = getSuccessRes(data=data)
    return jsonify(res)



